package com.yanqu.road.server.cmd.activity.springunionbanquet;

import com.yanqu.road.entity.activity.springunionbanquet.config.SpringUnionBanquetAwardInfo;
import com.yanqu.road.entity.activity.springunionbanquet.config.SpringUnionBanquetConfig;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.union.cross.CrossUnionInfo;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.SpringUnionBanquetProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.CmdApi;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.springunionbanquet.SpringUnionBanquetMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.server.pb.PlayerPb;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.Map;

@CmdApi(reqMsg = SpringUnionBanquetProto.SpringUnionBanquetOpenReqMsg.class, respMsg = SpringUnionBanquetProto.SpringUnionBanquetJoinRespMsg.class)
@Cmd(code = Protocol.S_SPRING_UNION_BANQUET_OPEN, desc = "宴请")
public class SpringUnionBanquetOpenCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        // 没有活动
        SpringUnionBanquetConfig config = SpringUnionBanquetMgr.getConfig();
        if (config == null || !ActivityMgr.activityInTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }

        String unionUid = player.getUserInfo().getUnionUid();
        if(StringUtils.isNullOrEmpty(unionUid)){
            ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
            return;
        }

        CrossUnionInfo unionInfo = CrossUnionMgr.getUnionInfo(unionUid);
        if(unionInfo == null){
            ret = GameErrorCode.E_UNION_NO_FOUND;
            return;
        }

        if(!SystemOpenMgr.systemOpen(player, eSystemId.SpringBanquet.getValue())){
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }

        SpringUnionBanquetProto.SpringUnionBanquetOpenReqMsg reqMsg = SpringUnionBanquetProto.SpringUnionBanquetOpenReqMsg.parseFrom(packet.getBytes());
        int goodsId = reqMsg.getGoodsId();
        GoodsInfo goodsInfo = GoodsMgr.getGoodsById(goodsId);
        if(goodsInfo == null){
            ret = GameErrorCode.E_GOODS_NO_EXIST;
            return;
        }

        SpringUnionBanquetAwardInfo awardInfo = config.getSpringUnionBanquetAwardInfo(goodsId);
        if(awardInfo == null){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }

        Map<Long, CrossUnionMember> unionMemberMap = CrossUnionMgr.getUnionMemberMap(unionUid);
        if(unionMemberMap == null){
            ret = GameErrorCode.E_UNION_NO_FOUND;
            return;
        }
        int unionMaxMemberSize = unionMemberMap.size();

        //扣除道具
        Property consume = PropertyHelper.parseStringToProperty(goodsId + "=1");
        if(!player.getModule(CurrencyModule.class).currencyIsEnough(consume)){
            ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
            return;
        }

        player.getModule(PreReduceModule.class).preReduce(consume);

        SpringUnionBanquetProto.CrossSpringUnionBanquetOpenReqMsg.Builder crossReq = SpringUnionBanquetProto.CrossSpringUnionBanquetOpenReqMsg.newBuilder();
        crossReq.setActivityId(config.getActivityId());
        crossReq.setUnionUid(unionUid);
        crossReq.setGoodsId(goodsId);
        crossReq.setUserBaseInfo(PlayerPb.parsePlayerBaseTempMsg(player.getUserInfo()));
        crossReq.setMaxMemberCount(unionMaxMemberSize);
        for (Map.Entry<Long, CrossUnionMember> entry : unionMemberMap.entrySet()) {
            crossReq.addMember(entry.getKey());
        }
        player.sendPacket(Protocol.C_SPRING_UNION_BANQUET_OPEN, crossReq);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            player.sendPacket(Protocol.U_SPRING_UNION_BANQUET_OPEN, SpringUnionBanquetProto.SpringUnionBanquetOpenRespMsg.newBuilder().setRet(ret));
        }
    }
}
